package comunicacao;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;
import java.util.ArrayList;

public class ObservadorTCP implements Comunicacao{

	private MulticastSocket multiSocket;
	private ServerSocket serverSocket;
	private int port;
	private ArrayList<String> listaServidores;
	private ThreadReceberInformacoesServico recInfo;
	
	public MulticastSocket getMultiSocket() {
		return multiSocket;
	}

	public void setMultiSocket(MulticastSocket multiSocket) {
		this.multiSocket = multiSocket;
	}

	public ObservadorTCP(int port) throws IOException {
		this.multiSocket = new MulticastSocket(port);
		this.setPort(port);
		this.setListaServidores(new ArrayList<String>());
		this.setServerSocket(new ServerSocket(port));
		this.recInfo = new ThreadReceberInformacoesServico(this);
		this.recInfo.start();
	}
	
	@Override
	public void enviar(Mensagem mensagem) throws DestinoDesconhecidoException,
			IOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void receber(Mensagem mensagem) throws DestinoDesconhecidoException,
			IOException {
		if(mensagem instanceof MensagemInformacoesServico) {
			System.out.println("Recebid a informacao do servidor!");
		}
		
	}
	
	@SuppressWarnings("deprecation")
	public void search(){
		// Which port should we send to
		int port = getPort();
		// Which address
		String group = "225.4.5.6";
		// Which ttl
		int ttl = 1;
		// Create the socket but we don't bind it as we are only going to send
		// data
		try {

			// Note that we don't have to join the multicast group if we are
			// only
			// sending data and not receiving
			MensagemSolicitacaoServico mss = new MensagemSolicitacaoServico(null, InetAddress.getLocalHost().getHostAddress());

			//
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(bos);
			oos.writeObject(mss);
			oos.flush();
			oos.close();
			bos.close();
			byte[] data = bos.toByteArray();
			//

			// Create a DatagramPacket
			DatagramPacket pack = new DatagramPacket(data, data.length,	InetAddress.getByName(group), port);
			// Do a send. Note that send takes a byte for the ttl and not an
			// int.
			multiSocket.send(pack, (byte) ttl);
			// And when we have finished sending data close the socket
			multiSocket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public ArrayList<String> getListaServidores() {
		return listaServidores;
	}

	public void setListaServidores(ArrayList<String> listaServidores) {
		this.listaServidores = listaServidores;
	}

	public ServerSocket getServerSocket() {
		return serverSocket;
	}

	public void setServerSocket(ServerSocket serverSocket) {
		this.serverSocket = serverSocket;
	}
	
	@SuppressWarnings("deprecation")
	public void close() throws IOException{
		this.multiSocket.close();
		this.serverSocket.close();
		this.recInfo.stop();
	}

}
